package com.ruoyi.tb.lmt.controller;

import java.util.Date;
import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.tb.lmt.domain.bo.TEquipmentBo;
import com.ruoyi.tb.lmt.domain.vo.TbEquipmentApplyVo;
import com.ruoyi.tb.lmt.service.ITEquipmentService;
import com.ruoyi.tb.lmt.service.ITbEquipmentApplyService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tb.lmt.domain.vo.TbStorageVo;
import com.ruoyi.tb.lmt.domain.bo.TbStorageBo;
import com.ruoyi.tb.lmt.service.ITbStorageService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出入库申请
 *
 * @author ruoyi
 * @date 2024-04-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xha-lmt/storage")
public class TbStorageController extends BaseController {

    private final ITbStorageService iTbStorageService;

    @Autowired
    private ITEquipmentService iTEquipmentService;

    @Autowired
    private ITbEquipmentApplyService iTbEquipmentApplyService;

    /**
     * 查询出入库申请列表
     */
    @SaCheckPermission("xha-lmt:storage:list")
    @GetMapping("/list")
    public TableDataInfo<TbStorageVo> list(TbStorageBo bo, PageQuery pageQuery) {
        return iTbStorageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出出入库申请列表
     */
    @SaCheckPermission("xha-lmt:storage:export")
    @Log(title = "出入库申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TbStorageBo bo, HttpServletResponse response) {
        List<TbStorageVo> list = iTbStorageService.queryList(bo);
        ExcelUtil.exportExcel(list, "出入库申请", TbStorageVo.class, response);
    }

    /**
     * 获取出入库申请详细信息
     *
     * @param ID 主键
     */
    @SaCheckPermission("xha-lmt:storage:query")
    @GetMapping("/{ID}")
    public R<TbStorageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long ID) {
        return R.ok(iTbStorageService.queryById(ID));
    }

    /**
     * 新增入库申请
     */
    @SaCheckPermission("xha-lmt:storage:addIn")
    @Log(title = "入库申请", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/in")
    public R<Void> addIn(@RequestBody TbStorageBo bo) {
        bo.setStatus("入库");
        bo.setSubmissionTime(new Date());
        TbEquipmentApplyVo tbEquipmentApplyVo = iTbEquipmentApplyService.selectByCode(bo.getEquipmentCode());

        // 检查tbEquipmentApplyVo是否为null
        if (tbEquipmentApplyVo != null) {
            // 如果不为空，则进行后续的录入高价件管理表操作
            TEquipmentBo equipmentBo = new TEquipmentBo();
            equipmentBo.setEquipmentCode(tbEquipmentApplyVo.getEquipmentCode());
            equipmentBo.setEquipmentName(tbEquipmentApplyVo.getEquipmentName());
            equipmentBo.setAssetsCode(tbEquipmentApplyVo.getAssetsCode());
            equipmentBo.setLmtNumber(tbEquipmentApplyVo.getLmtNumber());
            equipmentBo.setLmtLength(tbEquipmentApplyVo.getLmtLength());
            equipmentBo.setStockindate(new Date());
            iTEquipmentService.insertByBo(equipmentBo);
        }

        // 无论tbEquipmentApplyVo是否为空，都执行此操作
        return toAjax(iTbStorageService.insertByBo(bo));
    }

    /**
     * 新增出库申请
     */
    @SaCheckPermission("xha-lmt:storage:addOut")
    @Log(title = "出库申请", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/out")
    public R<Void> addOut(@RequestBody TbStorageBo bo) {
        bo.setStatus("出库");
        bo.setSubmissionTime(new Date());
        return toAjax(iTbStorageService.insertByBo(bo));
    }

    /**
     * 修改出入库申请
     */
    @SaCheckPermission("xha-lmt:storage:edit")
    @Log(title = "出入库申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TbStorageBo bo) {

        return toAjax(iTbStorageService.updateByBo(bo));
    }

    /**
     * 删除出入库申请
     *
     * @param IDs 主键串
     */
    @SaCheckPermission("xha-lmt:storage:remove")
    @Log(title = "出入库申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] IDs) {
        return toAjax(iTbStorageService.deleteWithValidByIds(Arrays.asList(IDs), true));
    }
}
